{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f263564",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7240781",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "63f31645",
   "metadata": {},
   "source": [
    "对于ndarray结构来说，里面所有的元素必须是同一类型，如果不是的话，会自动的向下进行转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cb80198a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_list =[1,2,3,4,5]\n",
    "tang_array = np.array(tang_list)\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "72c04e51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(tang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "79b15d72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0f7026cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('<U11')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fe3b565f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.itemsize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f3f4e6dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b3302aa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.size(tang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b7d112ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "223f0025",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.fill(0)\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "655f282c",
   "metadata": {},
   "source": [
    "索引与切片:跟python都是一样的\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fe88ffa2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_list =[1,2,3,4,5]\n",
    "tang_array = np.array(tang_list)\n",
    "tang_array[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b93d8676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "03562a66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array[-2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "910a07dd",
   "metadata": {},
   "source": [
    "矩阵格式（多维的形式）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9842bac6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d0f1f8ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "87d6488b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "56b1546b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6635bebf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 5, 8])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array[:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ed661a65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fe674156",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array2=tang_array\n",
    "tang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "25d449aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang_array2[1,1]=100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f64378f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "32fd708e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6c5b6508",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array2=tang_array.copy()\n",
    "tang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "bf5e4209",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[    1,     2,     3],\n",
       "       [    4, 10000,     6],\n",
       "       [    7,     8,     9]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array2[1,1]=10000\n",
    "tang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6b2539d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c7641aa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array =np.arange(0,100,10)\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "41efa5bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True,  True, False, False,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = np.array([0,0,0,1,1,1,0,0,2,2],dtype=bool)\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "68bdb1ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([30, 40, 50, 80, 90])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array[mask]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "cff1955c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.10785031, 0.44503212, 0.48231838, 0.51701252, 0.25669027,\n",
       "       0.39300603, 0.89829963, 0.64256954, 0.40719215, 0.52426197])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array = np.random.rand(10)\n",
    "random_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "56e9fc3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True, False, False,  True,  True, False,\n",
       "        True])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = random_array > 0.5\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "ee2e19b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([30, 60, 70, 90])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array[mask]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "01ffbc08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array = np.array([10,20,30,40,50])\n",
    "tang_array > 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9e65ff67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([3, 4], dtype=int64),)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(tang_array > 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0bc0e83e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([40, 50])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array[np.where(tang_array > 30)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d2a3752",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
